package ch.zbw.rre.binaeresSuchen;

import java.util.Random;


public class Listework {
	
	private final int MAX = 500;
	private int liste[] = new int[MAX];
	private int ptr;
	
	public Listework ()
	{
		ptr = 0;
	}
	
	public void show()
	{
		for(int i=0; i < ptr; i++)
		{
			System.out.print(liste[i] + " ");
			if (i%20 == 0 && i != 0)
				System.out.println("");
			
		}
		System.out.println("");
	}
	
	public void sort()
	{
		int right = ptr;
		int h;
		
		while (right >1)
		{
			for (int i = 0 ; i < right-1; i++)
			{
				if (liste[i] > liste[i+1])
				{
					// tausche 
					h = liste[i];
					liste[i] = liste[i+1];
					liste[i+1]=h;
				}
			}
			right--;
		}
	}
	
	public void insertElement(int anz)
	{
		int value;
		if (anz >= MAX)
			ptr = MAX;
		else
			ptr = anz;
			
		for (int i=0; i < anz; i++)
		{
			value = new Random().nextInt(1000);
			liste[i]= value;
		}
	}
	
	public int searchlinear(int w){
	int temp = 0;
	for (int i = 0; i < this.liste.length; i++){
		if(this.liste[i] == w){
			temp = i;
			i = this.liste.length;
			}
	}
	if (temp > 0)
		return temp; // Element gefunden
	else
		return -1; // Element nicht gefunden
	
	}
	
		

	
	public int searchbinaer(int w){
		int links = 0;
		int rechts = this.MAX;
		int mitte = 0;
		boolean control = false;
		
		while (control != true){
			mitte = (links+rechts)/2;
			
			if (this.liste[mitte] == w){
				control = true;
				return mitte;
			}
			if(links>rechts){
				control = true;
			}
			
			else {
				if (w < this.liste[mitte]){
					rechts = mitte-1;
				}
				else{
					links = mitte+1;
				}
			}
		}
		if (w == this.liste[mitte]){
			return mitte; // Element gefunden
		}
		else{
			return -1; // Element nicht gefunden
		}
	}
}


